package com.huxiu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.huxiu.utils.CacheUtils;
import com.huxiu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity implements OnClickListener {
	private ViewPager vp_guide_bg;
	private List<ImageView> imgs;
	private LinearLayout ll_guide_points;
	private ImageView iv_guide_redPoint;
	private Button bt_guide_start;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_guide);
		init();
	}

	private void init() {
		vp_guide_bg = (ViewPager) findViewById(R.id.vp_guide_bg);
		ll_guide_points = (LinearLayout) findViewById(R.id.ll_guide_points);
		iv_guide_redPoint = (ImageView) findViewById(R.id.iv_guide_redPoint);
		bt_guide_start = (Button) findViewById(R.id.bt_guide_start);
		bt_guide_start.setOnClickListener(this);
		// 初始化数据
		initData();
		// 设置适配器
		vp_guide_bg.setAdapter(new Myadapter());
		// 监听ViewPager的滑动
		vp_guide_bg.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	class MyOnPageChangeListener implements OnPageChangeListener {
		// ViewPager移动时回调
		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			System.out.println("position:" + position + ":positionOffset:"
					+ positionOffset + ":positionOffsetPixels:"
					+ positionOffsetPixels);
			// positionOffset 就是手指移动的距离与屏幕宽度的比
			// 红点移动的距离= 灰点的间距 * positionOffset
			int redPointX = (int) (dp2px(20) * (positionOffset+position));
			// 通过设置红点的leftMargin值让红点移动
			android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) iv_guide_redPoint.getLayoutParams();
			params.leftMargin = redPointX;
			iv_guide_redPoint.setLayoutParams(params);
		}

		@Override
		public void onPageSelected(int position) {
			// 当选中某一页时调用
			if(position==imgs.size()-1){
				bt_guide_start.setVisibility(View.VISIBLE);
			}else{
				bt_guide_start.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {

		}

	}

	class Myadapter extends PagerAdapter {

		@Override
		public int getCount() {
			return imgs.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ImageView imageView = imgs.get(position);
			container.addView(imageView);
			return imageView;
		}

	}

	private void initData() {
		int[] imgIds = new int[] { R.drawable.guide_1, R.drawable.guide_2,
				R.drawable.guide_3 , R.drawable.guide_4, R.drawable.guide_5};
		imgs = new ArrayList<ImageView>();
		for (int i = 0; i < imgIds.length; i++) {
			ImageView imageView = new ImageView(getApplicationContext());
			imageView.setImageResource(imgIds[i]);
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imgs.add(imageView);
			View view = new View(getApplicationContext());
			view.setBackgroundResource(R.drawable.guide_point_nomal);
			int dp2px = dp2px(10);
			LayoutParams params = new LayoutParams(dp2px, dp2px);
			if (i != 0) {
				params.leftMargin = dp2px;
			}
			view.setLayoutParams(params);
			ll_guide_points.addView(view);
		}
	}
	
	private int dp2px(int dp){
		// px = dp * 密度比
		float density = getResources().getDisplayMetrics().density;
		int px = (int) (dp * density + 0.5f);
		return px;
	}

	@Override
	public void onClick(View v) {
		CacheUtils.putBoolean(this, Utils.getVersionName(GuideActivity.this)+WelcomeActivity.IS_APP_OPENED, true);
		startActivity(new Intent(this,MainActivity.class));
		finish();
	}
}
